package agents;


import factoryGame.ObjectFactory;
import game.constants.ReferenceAgent;
import game.constants.ReferenceSpecialFeature;
import interfaces.impl.Agent;

import java.util.ArrayList;

import objects.SpetialFeature;
import objects.WorldObject;
import universe.Universe;
import actions.ActionCreateObject;
import constants.Constants;
public class AgentCreateObject extends Agent{
	
	SpetialFeature timeToRevive;
	SpetialFeature objectRefrenceToRevive;
	SpetialFeature maxNumber;
	
	ActionCreateObject actionCreate;
	
	double contTime=0;
	
	@Override
	public void initializeActionList() {
		actionCreate= new ActionCreateObject();
		listActions.add(actionCreate);
	}

	@Override
	public void initializeSpecialFeatures() {
		timeToRevive= new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_TIME_TO_REVIVE);
		listFeatures.add(timeToRevive);
		
		maxNumber= new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_MAX_NUMBER_OBJECTS_TO_CREATE);
		listFeatures.add(maxNumber);
		
		objectRefrenceToRevive=new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_OBJECT_REFERENCE_TO_REVIVE);
		listFeatures.add(objectRefrenceToRevive);
		
		
		
	}

	@Override
	public void initializeName() {
			name="Revive Object";
		
	}

	@Override
	public void initializeId() {
		this.id=ReferenceAgent.AGENT_CREATE_OBJECT;
		
	}

	@Override
	public void updateChild(WorldObject object, Universe universe, double time) {
		
		contTime=contTime+time;
		if(contTime<timeToRevive.getValue()){
			return;
		}
		
		ArrayList<WorldObject> list = universe.getListObjects((int) objectRefrenceToRevive.getValue());
		int number=0;
		for(WorldObject obj : list){
			for(SpetialFeature fe : obj.getListFeatures()){
				if(fe.getId()==ReferenceSpecialFeature.SPETIAL_FEATURE_CREATOR_ID){
					if(object.getID()==fe.getValue()){
						number++;
					}
				}
			}
		}
		
		if(number>maxNumber.getValue()){
			return;
		}
		
		SpetialFeature creatorId= new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_CREATOR_ID);
		creatorId.setName("Creator Id");
		creatorId.setValue(object.getID());
		
		WorldObject obj = ObjectFactory.createWorldObject((int)objectRefrenceToRevive.getValue(), Constants.NO_OWNER, object.getPosition().getX(), object.getPosition().getY(),Constants.ASSIGN_SPECIAL_FEATURE);
		obj.addSpetialFeature(creatorId);
		
		actionCreate.setSource(object);
		actionCreate.setTarget(object);
		actionCreate.setObjectToCreate(obj);
		universe.setAction(actionCreate);
		contTime=0;
	}

	
}
